<?php
/**
 * JsClasses Multiple Backends Edition
 * FrontEnd Class
 *
 * @copyright  2010 RIA Allegro
 * @license    GNU LGPL
 * @version    1.3.1 (2010-05-24)
 * @author     Vasiliy.Lyalyuk@gmail.com
 * @link       http://linux.ria.ua/JsClasses/
 */
class JsClassesFrontend  extends JsClassesMemcache{
    
    /**
     * Set Mamcache Time on jsClasses.jscache
     *
     * @var string
     */    
     private $_jscacheLifeTime=0;


    /**
     * make JS Classes
     *
     * @param array $classes array of JSClasses without '.js' extension.
     * @example
     *   new JsClasses(array('Ria_JsonRequest','RIA_HtmlRequest'));
     */
    public function make($classes) {
        While (memcache_get($this->_memcacheConnection, 'rebildFile')) {
            usleep(10000);    // wait for 1 seconds =1000000
        }
        memcache_set($this->_memcacheConnection, 'rebildFile','1',0, 3);
        $this->_loadClassesCache();
        $this->_parseClasses($classes);
        $this->_checkClasses();
            if (! file_exists($this->_getJsRealFileName())) {
                $this->_isCacheRebuildRequired = true;
            }
            if ($this->_isCacheRebuildRequired) {
                $this->_generateJsFileName();
                $this->_generateJsFile();
                $this->_generateJSCache();
                if ($this->_compressMode) {
                    $this->_makeCompressedJsFile();
                }
                if ($this->_gzipFileMode) {
                    $this->_gzipJsFile();
                }
            }
        memcache_delete($this->_memcacheConnection, 'rebildFile', 0);
    }

     /**
     * Set Life Mamcache Time on jsClasses.jscache
     *
     * @param string 
     */
    public function setJscacheLifeTime($jscacheLifeTime) {
    	$this->_jscacheLifeTime = $jscacheLifeTime;
    } 
    
    /**
     * Get path Virtual Cache JavaScript file
     *
     * @return string
     */
    public function getJsFileName() {
        return $this->_jsFileName;
    }

    /**
     * Generate JavaScript Classes list Mamcache-file
     *
     */
    protected function _generateJSCache() {
        $classesCacheFile = $this->_getClassesCacheFileName();
        $text=$this->_jsFileName."\n";
        foreach ($this->_classesCacheData as $className => &$class) {
             $text.=$className . ':' . $class['classPath'] . ':' . $class['lastModifyTime'] . ':'
                    .join(',', $class['perentClasses']) . "\n";
        }
        memcache_delete($this->_memcacheConnection, $classesCacheFile, 0);
        if (!memcache_set($this->_memcacheConnection, $classesCacheFile, $text ,0, $this->_jscacheLifeTime)) {
            $this->trigger_error("File ".$classesCacheFile." is not write", E_USER_ERROR);
        }
    }

}

?>